package com.qf.fmall.utils;

import cn.hutool.core.util.XmlUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

import java.util.*;

public class HutoolHttpUtils {

    public static void main(String[] args) {

        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";

        // 把hashmap转换为 xml字符串
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("appid","wx632c8f211f8122c6");
        hashMap.put("mch_id","1497984412");
        String nonce_str = UUID.randomUUID().toString().replaceAll("-","");
        System.out.println(nonce_str);
        hashMap.put("nonce_str",nonce_str);
        hashMap.put("body","零食");
        hashMap.put("out_trade_no","20240429143710001"); // 订单编号
        hashMap.put("total_fee","1");  // 1分钱
        hashMap.put("notify_url","http://47.118.45.73:8080/pay/callback");
        hashMap.put("trade_type","NATIVE");

        // 计算签名，把签名信息放入请求参数中
        String sign =  createSign(hashMap);

        // 签名
        hashMap.put("sign",sign);

        String s = XmlUtil.mapToXmlStr(hashMap);

        // 调用微信支付
        String body = HttpRequest.post(url)
                .body(s)
                .execute()
                .body();

//        System.out.println(body);

        // xml转换 map
        Map<String, Object> map = XmlUtil.xmlToMap(body);
        String payurl = (String) map.get("code_url");
        System.out.println(payurl);

//        testPost();

    }

    private static String createSign(HashMap<String, String> hashMap) {

        //1. 对map中的key进行升序排序
        Set<String> keySet = hashMap.keySet();

        //2. 对set中的key进行升序排序
        TreeSet<String> set = new TreeSet<>(keySet);

        //3. stringA="appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA";

        StringBuffer stringBuffer = new StringBuffer();
        for (String key : set) {
            String value = hashMap.get(key);
            stringBuffer.append(key);
            stringBuffer.append("=");
            stringBuffer.append(value);
            stringBuffer.append("&");
        }
        String stringA = stringBuffer.toString();

        String stringSignTemp=stringA+"key=sbNCm1JnevqI36LrEaxFwcaT0hkGxFnC"; //注：key为商户平台设置的密钥key

        String sign = DigestUtil.md5Hex(stringSignTemp).toUpperCase();

        return sign;
    }

    private static void testPost() {
        String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//        String content = HttpUtil.get(url);
//        System.out.println(content);

        String json ="<xml>\n" +
                "   <appid>wx2421b1c4370ec43b</appid>\n" +
                "   <attach>支付测试</attach>\n" +
                "   <body>JSAPI支付测试</body>\n" +
                "   <mch_id>10000100</mch_id>\n" +
                "   <detail><![CDATA[{ \"goods_detail\":[ { \"goods_id\":\"iphone6s_16G\", \"wxpay_goods_id\":\"1001\", \"goods_name\":\"iPhone6s 16G\", \"quantity\":1, \"price\":528800, \"goods_category\":\"123456\", \"body\":\"苹果手机\" }, { \"goods_id\":\"iphone6s_32G\", \"wxpay_goods_id\":\"1002\", \"goods_name\":\"iPhone6s 32G\", \"quantity\":1, \"price\":608800, \"goods_category\":\"123789\", \"body\":\"苹果手机\" } ] }]]></detail>\n" +
                "   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>\n" +
                "   <notify_url>https://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>\n" +
                "   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>\n" +
                "   <out_trade_no>1415659990</out_trade_no>\n" +
                "   <spbill_create_ip>14.23.150.211</spbill_create_ip>\n" +
                "   <total_fee>1</total_fee>\n" +
                "   <trade_type>JSAPI</trade_type>\n" +
                "   <sign>0CB01533B8C1EF103065174F50BCA001</sign>\n" +
                "</xml>";
        HttpResponse response = HttpRequest.post(url)
                .body(json) // 设置请求体中的数据
                .execute();
        String result2 = response // 发送请求并且接收响应
             .body(); // 响应体的内容

        System.out.println(result2);
    }

}
